<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class PointService extends BaseService
{
    //商城二期

    protected $types = [
        '', '签到', '下单', '充值', '退还',
    ];

    /**
     * 列表
     * @return array
     */
    public function getList($type = 0, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbEqJifen)->where('del', 0);
        if ($type) {
            $db = $db->where('type', $type);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(0, '');
        }
//        $types = $this->types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
//            $tname = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $uOne = (array) $this->dbConn($this->dbUser)->where('id', $v['uid'])->first();
            $uname = !empty($uOne)&&$uOne['nickname'] ? $uOne['nickname'] : '-';
            $arys[] = [
                'id' => $v['id'],
//                'type' => $v['type'],
//                'tname' => $tname,
                'uname' => $uname,
                'reason' => $v['reason'],
                'num' => $v['operator'].$v['change_points'],
                'addtime' => $v['addtime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 兑换列表
     * @return array
     */
    public function orderList($pages)
    {
        (new \App\Service\ApiXcx\PointsOrderService())->autoAccept();
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = DB::table($this->dbEqJfOrder)->where('del', 0);
        $total = $db->count();
        $db = $db->skip($start)->take($pages['limit']);
        $res = $db->orderBy('id', 'desc')->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $skuInfo = '';
            $skuTotal = $this->dbConn($this->dbPointsOsku)
                ->where('del', 0)
                ->where('order_id', $v['id'])
                ->count();
            $skuOne = (array) $this->dbConn($this->dbPointsOsku)
                ->where('del', 0)
                ->where('order_id', $v['id'])
                ->orderBy('id', 'asc')
                ->first();
            if (!empty($skuOne)) {
                $skuInfo .= '商品名称：'.$skuOne['gname'];
                $skuInfo .= '【'.$skuOne['sku_name'].'】';
                if (substr($skuOne['gimg'], 0, 10) == 'data:image') {
                    $skuInfo .= '<img src="'.$skuOne['gimg'].'" style="height: 30px;"/>';
                } else if (file_exists($skuOne['gimg'])) {
                    $skuInfo .= '<img src="/'.$skuOne['gimg'].'" style="height: 30px;"/>';
                }
            }
            if ($skuTotal > 1) {
                $skuInfo .= '...';
            }
            $arys[] = [
                'id' => $v['id'],
                'uname' => $v['uname'],
                'orderno' => $v['orderno'],
                'num' => $v['num'],
                'skuInfo' => $skuInfo,
                'addtime' => $v['addtime'],
                'iscancel' => $v['iscancel'],
                'status' => $v['status'],
                'wuliu' => $v['wuliu'],
                'wuliuhao' => $v['wuliuhao'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    //审核
    public function auth($id)
    {
        $res = (array) $this->dbConn($this->dbEqJfOrder)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($res['iscancel'] != 2) {
            return $this->toback(-2, '状态错误');
        }
        $up = $this->dbConn($this->dbEqJfOrder)->where('id', $id)
            ->update(['iscancel' => 3, 'cancelauthtime' => toDate()]);
        if (!$up) {
            return $this->toback(-2, '操作失败');
        }
        //积分退回
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $res['uid'])
            ->first();
        $pointNum = empty($uOne) ? 0 : $uOne['points'];
//        $pointNew = $pointNum > $res['real_pay'] ? $pointNum - $res['real_pay'] : 0;
        $pointNew = $pointNum + $res['real_pay'];
        $this->dbConn($this->dbUser)->where('id', $res['uid'])->update(['points' => $pointNew]);
        //退还记录
        $point_log = [
            'uid' => $res['uid'], 'type' => 4,
            'before_points' => $pointNum,
            'change_points' => $res['real_pay'],
            'after_points' => $pointNew,
            'operator' => '+',
            'reason' => '退还积分',
            'relation_value' => '',
            'addtime' => toDate()
        ];
        $this->dbConn($this->dbUserPointLog)->insertGetId($point_log);
        return $this->toback();
    }

    //审核
    public function send($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbEqJfOrder)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['status'] = 3;
        $pas['sendtime'] = toDate();
        $res = $this->dbConn($this->dbEqJfOrder)->where('id', $id)->update($pas);
        if (!$res) {
            return $this->toback(-1, '操作失败');
        }
        return $this->toback();
    }
}